"""
服务中API和任务的方法映射
"""
from sqlalchemy.exc import SQLAlchemyError
import traceback
import json
import requests

from api.base import *
from util import log

from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

from service import mask2Nii
from service import nii2Mask

def call(params):
    logger = log.lgtest()
    logger.lognow('request: ' + str(params), 'info', 'request_response')
    response = ''
    try:
        params = json.loads(params)
        method = params["method"]
        if method == "MASK_2_NII":
            handel = mask2Nii.Mask2Nii()
            response = handel.process(params['data'])
        elif method == "NII_2_MASK":
            handel = nii2Mask.Nii2Mask()
            response = handel.process(params['data'])
        return response
    except (TypeError, KeyError):
        msg = 'TypeError Or KeyError: %s' % traceback.format_exc()
        logger.lognow(msg, 'error', 'error')
        response = BaseClass.message(code=1101)
        return response
    except json.decoder.JSONDecodeError:
        msg = 'JsonDecodeError: %s' % str(params)
        logger.lognow(msg, 'error', 'error')
        response = BaseClass.message(code=1103)
        return response
    except SQLAlchemyError:
        msg = 'SQLAlchemyError: %s' % traceback.format_exc()
        logger.lognow(msg, 'error', 'error')
        response = BaseClass.message(code=1104)
        return response
    except Exception:
        msg = 'General Exception: %s' % traceback.format_exc()
        logger.lognow(msg, 'error', 'error')
        response = BaseClass.message(code=1001)
        return response
    finally:
        logger.lognow('response: ' + str(response), 'info', 'request_response')

